package org.future.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientHandler implements Runnable{
	private Socket socket;
	private BufferedReader in;
	private PrintWriter out;
	
	public ClientHandler(Socket socket)throws IOException{
		this.socket = socket;
		in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		out = new PrintWriter(socket.getOutputStream());
	}
	
	public void run() {
	    try{
	    	out.println("hi baby.");
	    	out.println("bye");
	    	out.flush();            // 此处的flush()非常重要，否则服务端无法收到消息
	    	String msg = null;
	    	while((msg = in.readLine()) != null){
	    		System.out.println(msg);
	    		if(msg.indexOf("bye") > -1){
	    			break;
	    		}
	    	}
	    } catch(IOException e){
	    	e.printStackTrace();
	    } finally{
	    	try{
	    		if(socket != null){
		    		socket.close();
		    	}
	    	} catch(Exception e){
	    	}	    	
	    }
	}
}
